/**
 * 
 */
package com.android.hw.projectmenu;

import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import com.qualcomm.qcnvitems.QcNvItems;

/**
 * @author gaoshengpan
 * 
 */
public class ShowHySNNumber extends PreferenceActivity {
	/*15-09-08 gaoshenpan add secrect code for hy_sn begin*/
    private QcNvItems mNv;
    private static String mUnknown = "null";
    private static final String KEY_SN_NUM = "sn_num";
    private static final String KEY_SN_NUM1 = "sn_num1";
    private String TAG = "ShowHySNNumber";
    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            switch (msg.what) {

            case 0:

                break;
            case 1:
                /* 15-09-08 gaoshenpan fix just show hwSN begin */
                // setSummaryText(KEY_SN_NUM, readSN(0));
                setSummaryText(KEY_SN_NUM, readSN(1));
                /* 15-09-08 gaoshenpan fix just show hwSN end */
                break;
            default:
                break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.hy_sn_num);
        mNv = new QcNvItems(this);

    }

    /**
     * 
     */
    public ShowHySNNumber() {
        // TODO Auto-generated constructor stub
    }

    public String getSN() {
        String info = null;
        if (mNv == null) {
            mNv = new QcNvItems(this);
        }
        if (mNv != null) {
            mHandler.sendEmptyMessage(1);
        }

        if (info == null) {
            return mUnknown;
        }
        return info;
    }

    private String readSN(int i) {
        String sn = null;
        try {
            /* 15-09-08 gaoshenpan fix just show hwSN begin */
            /* sn = mNv.getSNNumber1().substring(0, 31); */
            sn = mNv.getSNNumber().substring(0, 31);
            /* 15-09-08 gaoshenpan fix just show hwSN end */
            Log.d(TAG, "readSN -----:" + sn);
        } catch (Exception e) {
            // TODO: handle exception
            Log.e(TAG, "getSN e:" + e);
        }
        Log.e(TAG, "getSN info :" + sn);
        return sn;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.preference.PreferenceActivity#onDestroy()
     */
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (mNv != null) {
            mNv.dispose();
        }
    }

    private void setSummaryText(String preferenceKey, String value) {
        Preference preference = findPreference(preferenceKey);
        if (preference == null)
            return;

        if (TextUtils.isEmpty(value))
            preference.setSummary(mUnknown);
        else
            preference.setSummary(value);
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        getSN();
    }
    /*15-09-08 gaoshenpan add secrect code for hy_sn end*/
}
